package com.xxmw.transaction.controller.app;


import com.alibaba.fastjson.JSON;
import com.xxmw.transaction.common.annotation.IpAddress;
import com.xxmw.transaction.common.annotation.LoginUser;
import com.xxmw.transaction.common.annotation.UserLoginIgnore;
import com.xxmw.transaction.common.entity.R;
import com.xxmw.transaction.common.utils.Constant;
import com.xxmw.transaction.common.utils.JWTUtil;
import com.xxmw.transaction.model.app.User;
import com.xxmw.transaction.service.app.MessageService;
import com.xxmw.transaction.service.app.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.HashMap;

@Validated
@RestController
@RequestMapping("/app/user")
@Api(tags = "用户相关接口")
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private MessageService messageService;

	@Autowired
	private RedisTemplate<String, String> redisTemplate;

	/**
	 * 登陆
	 * @param ip 登录ip
	 * @param username 用户名
	 * @param password 密码
	 * @return
	 */
	@PostMapping("/login")
	@UserLoginIgnore
	@ApiOperation("用户登录")
	public R login(@IpAddress String ip,
			@NotBlank(message = "用户名或密码不能为空") @RequestParam String username,
			@NotBlank(message = "用户名或密码不能为空")@RequestParam String password) {
		User user = userService.login(username, password, ip);
		user.setPassword("");
		String token = JWTUtil.generate(JSON.toJSONString(user));
		HashMap<String, Object> result = new HashMap<>();
		result.put("token", token);
		user.setPassword("");
		user.setSalt("");
		if (user.getVipExpirationDate() != null && user.getVipExpirationDate().compareTo(new Date()) > 0 ) {
			user.setIsVip(User.VipStatus.IS_VIP.value());
		} else {
			user.setIsVip(User.VipStatus.NOT_VIP.value());
		}
		result.put("userInfo", user);
		return R.ok(result);
	}

	/**
	 * 注册(用户注册完成后由前端调起登陆接口)
	 * @param ip
	 * @param username 用户怕
	 * @param password 密码
	 * @param code
	 * @return
	 */
	@PostMapping("/register")
	@UserLoginIgnore
	@ApiOperation(value = "用户注册")
	public R register(@IpAddress String ip,
			@NotBlank(message = "用户名不能为空") @RequestParam String username,
			@NotBlank(message = "密码不能为空") @RequestParam String password,
			@NotBlank(message = "验证码不能为空") @RequestParam String code) {
		final String rcode = redisTemplate.opsForValue().get(username + Constant.Text.SIGN);
		if (StringUtils.isBlank(rcode) || !rcode.equals(code)) {
			return R.error("验证码不正确");
		}
		if (userService.getUser(username) != null) {
			return R.ok("该账号已被注册");
		}
		userService.saveUser(username, password, ip);
		messageService.clean(username, Constant.Text.SIGN);
		return R.ok();
	}

	/**
	 * 修改密码
	 * @param username
	 * @param newPassword
	 * @param code
	 * @return
	 */
	@PostMapping("/update")
	@UserLoginIgnore
	@ApiOperation("修改密码")
	public R update(@NotBlank(message = "用户名或密码不能为空") @RequestParam String username,
			@NotBlank(message = "验证码不为空") @RequestParam String code,
			@NotBlank(message = "用户名或密码不能为空") @RequestParam String newPassword) {
		if(!messageService.verify(username, Constant.Text.UPDATE, code)){
			return R.error("验证码错误");
		}
		userService.modifyPass(username, newPassword);
		messageService.clean(username, Constant.Text.UPDATE);
		return R.ok();
	}

	/**
	 * 获取用户信息
	 * @return
	 */
	@GetMapping("/info")
	@ApiOperation("获取用户信息")
	public R info(@ApiIgnore @LoginUser User user) {
		if (user.getVipExpirationDate() != null && user.getVipExpirationDate().compareTo(new Date()) > 0 ) {
		    user.setIsVip(User.VipStatus.IS_VIP.value());
        } else {
		    user.setIsVip(User.VipStatus.NOT_VIP.value());
        }
		return R.ok(user);
	}


	/**
	 * 登出
	 * @return
	 */
	public R logout() {
		return R.ok();
	}

	/**
	 * 忘记密码
	 * @param username
	 * @param code
	 * @param newPass
	 * @return
	 */
	@PostMapping("/forgotPass")
	@UserLoginIgnore
	@ApiOperation("忘记密码")
	public R forgotPass (@NotBlank(message = "手机号不能为空") @RequestParam String username,
						 @NotBlank(message = "验证码") @RequestParam String code,
						 @NotBlank(message = "密码不能为空") @RequestParam String newPass) {

		final String rCode = redisTemplate.opsForValue().get(username + Constant.Text.OTHER);
		if (StringUtils.isBlank(rCode) || !rCode.equals(code)) {
			return R.error("验证码不正确");
		}

		userService.modifyPass(username, newPass);

		return R.ok();
	}
}
